<template>
  <div class="tags">
    <el-tag
      v-for="(item, index) in tabList"
      :key="item.path"
      :closable="item.name !== 'home'"
      :effect="$route.name === item.name ? 'dark' : 'plain'"
      @click="changeMenu(item)"
      @close="handleClose(item, index)"
      size="small"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
export default {
  name: "CommonTag",
  computed: {
    ...mapState("tab", ["tabList"]),
  },
  methods: {
    ...mapMutations("tab", { closeTag: "closeTag" }),
    changeMenu(item) {
      this.$router.push(item.path);
    },
    handleClose(item, id) {
      this.closeTag(id);
      const length = this.tabList.length;
      if (item.name !== this.$route.name) {
        return;
      }
      if (id === length) {
        this.$router.push(this.tabList[id - 1].path);
      } else {
        this.$router.push(this.tabList[id].path);
      }
    },
  },
};
</script>

<style lang='less'>
.tags {
  padding: 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>